package com.ikkong.platform.controller;

import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.support.BladePage;
import com.ikkong.platform.meta.intercept.BlogIntercept;
import com.ikkong.platform.model.Blog;
import com.ikkong.platform.service.BlogService;
import com.ikkong.platform.service.impl.BlogServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.system.util.CommonUtil;
import com.ikkong.system.util.GridUtil;
import com.ikkong.system.util.PageResultBean;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.expr.ast.IncDec;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generated by Blade.
 * 2016-10-08 15:59:45
 */
public class BlogController extends UrlPermissController {
	private static String CODE = "blog";
	private static String PERFIX = "tb_tfw_blog";
	private static String LIST_SOURCE = "Blog.view";
	private static String BASE_PATH = "/platform/blog/";
	private static String ENJOY_PATH= "/WEB-INF/view/platform/blog/";
	
	BlogService service = new BlogServiceImpl();
	
	public void index() {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		String columnNames = "['ID','客户名称']";
		context.put("gridId", "BLOG_LIST");
		context.put("reurl", "/blog/list");
		context.put("columnNames", columnNames);
		CommonUtil.createFieldNameList(fieldNameList, "f_it_id", "30", "left", "", false, false);
		CommonUtil.createFieldNameList(fieldNameList, "f_it_title", "100", "left", "", false, true);
//		CommonUtil.createFieldNameList(fieldNameList, "phone", "100", "left", "", false, false);
//		CommonUtil.createFieldNameList(fieldNameList, "address", "100", "left", "", false, false);
//		CommonUtil.createFieldNameList(fieldNameList, "remark", "100", "left", "", false, false);

		context.put("fieldNameList", fieldNameList);

		String script = GridUtil.generateScript(context);

		setAttr("script", script);

		render("list.html");
		renderTemplate("/WEB-INF/view/platform/blog/blog.html");
	}
	public void script() {
//		int page = getParaToInt("page", 1);
//		int rows = getParaToInt("rows", 1);
//		List<Blog> pageBean = service.findAll();
//
//		String[] fieldNames = { "f_it_id", "f_it_title"};
//		PageResultBean pageResultBean = CommonUtil.convertPageBeanToPageResultBean(pageBean, fieldNames, null);
//		String data = JsonKit.toJson(pageResultBean);
//
//		renderJson(data);
	}
	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "blog_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Blog blog = service.findById(id);
		setAttr("model", JsonKit.toJson(blog));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "blog_edit.html");
	}

	public void view() {
		String id = getPara("f_it_title");
		List l = new ArrayList();
		if(id==null || id =="undefined"){
			l=service.findAll();
		}else {
			String where = "f_it_title="+id;
			l = service.findBy(where,Blog.class);
		}
		renderJson(l);
	}

	public void list() {
		String id = getPara("f_it_title");
		List<Blog> l = new ArrayList();
		if(id==null || id =="undefined"){
			Object grid = paginate("Blog.list",new BlogIntercept());
			renderJson(grid);
		}else {
			String where = "f_it_title="+id;
			l = service.findBy(where,Blog.class);
			renderJson(l);
		}

	}


	public void save() {
		Blog blog = mapping(PERFIX, Blog.class);
		boolean temp = service.save(blog);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Blog blog = mapping(PERFIX, Blog.class);
		blog.setVersion(blog.getVersion()+1);
		boolean temp = service.update(blog);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = Blade.create(Blog.class).deleteByIds(ids);
		if (cnt > 0) {
			renderJson(Ret.ok("msg", "客户信息删除成功"));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
